<?php

namespace App\Http\Controllers\Mobile\Company;
use App\Facades\CustomPassword;
use App\Facades\Site;
use App\Facades\Sms;
use App\Http\Requests\Common\Captcha\MobileRequest;
use App\Http\Requests\Company\ModPassword\FindPasswordRequest;
use App\Http\Requests\Company\ModPassword\MobileFindRequest;
use App\Models\Company\CompanyPasswordResets;
use Illuminate\Http\Request;
use App\Models\Company\CompanyUser;
use Illuminate\Support\Facades\Mail;
use Style;
use App\Models\Common\WebContent;

class PasswordController extends CommonController{

    /**
     * 企业找回密码
     * @return mixed
     */
    public function find(){
        return Style::view("mobile.company.find");
    }

    /**
     * 企业找回密码2
     * @return mixed
     */
    public function find2(Request $request){
        $loginUrl=$this->gotoMail($request->email);
        $email=$request->email;
        return Style::view("mobile.company.find2",compact('loginUrl','email'));
    }

    /**
     * 企业找回密码3
     * @return mixed
     */
    public function find3(Request $request){
        $cTime = date("Y-m-d H:i:s",strtotime("-1 day"));
        $companyPasswordResets=CompanyPasswordResets::where('token',$request->token)->where('created_at','>',$cTime)->first();
        //dd($companyPasswordResets);
        $token=$request->token;
        return Style::view("mobile.company.find3",compact('companyPasswordResets','token'));
    }

    /**
     * 企业找回密码4
     * @return mixed
     */
    public function find4(Request $request){
        $username=$request->username;
        return Style::view("mobile.company.find4",compact('username'));
    }

    /***
     *
     */
    public function set(FindPasswordRequest $request){
        $cTime = date("Y-m-d H:i:s",strtotime("-1 day"));
        $companyPasswordResets=CompanyPasswordResets::where('token',$request->token)->where('created_at','>',$cTime)->first();
        if(!empty($companyPasswordResets->uid)){
            $companyUser=CompanyUser::where('uid',$companyPasswordResets->uid)->first();
            $companyUser->password=CustomPassword::make($request->reset_password);
            $companyUser->save();
            $companyPasswordResets->delete();//删除找回密码列，让链接失效
//            return redirect('company/find_password4')->with('username', $companyUser->username);
            return response()->json(['status'=>0,'msg'=>'修改密码成功','username'=>$companyUser->username]);
        }else{
            return response()->json(['status'=>1,'msg'=>'修改密码失败，链接已失效！']);
        }
    }

    /**
     * email验证
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function emailFind(Request $request){
        $username = $request->input('username');
        $email = $request->input('email');
        $result = CompanyUser::where('username', $username)->first();
        if(empty($result['username'])){
            return response()->json(['status'=>1,'msg'=>'用户名不存在']);
        }elseif($result['email']!=$email){
            return response()->json(['status'=>1,'msg'=>'用户名和邮箱不匹配']);
        }else{
            $token=str_random(60);
            $cTime = date("Y-m-d H:i:s",strtotime("-1 day"));
            $count=CompanyPasswordResets::where('uid',$result->uid)->where('created_at','>',$cTime)->count();
            if($count>5){
                return response()->json(['status'=>1,'msg'=>'24小时内已经超出最大密码重置数']);
            }
            $companyPasswordResets = new companyPasswordResets;
            $companyPasswordResets->uid = $result->uid;
            $companyPasswordResets->token = $token;
            $companyPasswordResets->created_at = date("Y-m-d H:i:s");
            $companyPasswordResets->save();

            if(!empty(Site::get('MailAddress'))){
                $result = Mail::send('emails.mobile_company_find_password', ['user' => $result,'token'=>$token], function ($m) use ($request) {
                    $m->from(Site::get('MailAddress'), Site::get('webname'));
                    $m->to($request->email)->subject(Site::get('webname').'企业密码重置');
                });
                if($result){
//                    return redirect('company/find_password2?email='.$email)->with('email',$email);
                    return response()->json(['status'=>0,'msg'=>'邮件发送成功','email'=>$email]);
                }else{
                    return response()->json(['status'=>1,'msg'=>'邮件发送失败']);
                }
            }else{
                return response()->json(['status'=>1,'msg'=>'邮件发送失败']);
            }
        }
    }

    /**
     * 手机找回密码生成
     * @param MobileFindRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function mobileFind(MobileFindRequest $request){
        $mobile = $request->input('mobile');
        $result = CompanyUser::where('mobile', $mobile)->first();
        $request->session()->forget('mobile_verification');
        $request->session()->forget('mobile_verification_code');
        if(empty($result['mobile'])){
            return response()->json(['status'=>1,'msg'=>'这个手机号码还没有注册过!']);
        }else{
            $token=str_random(60);
            $cTime = date("Y-m-d H:i:s",strtotime("-1 day"));
            $count=CompanyPasswordResets::where('uid',$result->uid)->where('created_at','>',$cTime)->count();
            if($count>5){
                return back()->withInput()->with('status', '24小时内已经超出最大密码重置数！');
            }
            $companyPasswordResets = new companyPasswordResets;
            $companyPasswordResets->uid = $result->uid;
            $companyPasswordResets->token = $token;
            $companyPasswordResets->created_at = date("Y-m-d H:i:s");
            $companyPasswordResets->save();
            return response()->json(['status'=>0,'msg'=>'验证成功!','token'=>$token]);
//            return redirect('company/find_password3?token='.$token);
        }
    }

    /**
     * 获取短信验证码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCode(MobileRequest $request){
        $mobileNum = $request->input('mobile');
        $result = CompanyUser::where('mobile', $mobileNum)->first();
        if ($result) {
            //设置session
            $request->session()->put('mobile_verification_code', rand(1000, 9999));
            $request->session()->put('mobile_verification', $mobileNum);
            //模板参数
            $smsParam = array(
                'product' => Site::get('webname'),
                'code'    => (string)session('mobile_verification_code')
            );
            $sendResult = Sms::codeSend($mobileNum, $smsParam);
            //返回对象
            if (is_object(@$sendResult->result) && $sendResult->result->err_code == '0') {
                return response()->json(['status' => '1', 'msg' => '发送短信成功']);
            } else {
                return response()->json(['status' => '0', 'msg' => '一分钟之内只能发一次,或者您发送的次数已经达到上限']);
            }
        } else {
            return response()->json(['status' => '2', 'msg' => '没有找到此手机的注册信息！']);
        }
    }

    /**
     * 生成邮箱登陆地址
     * @param $mail
     * @return string
     */
    protected function gotoMail($mail){
        $t=explode('@',$mail);
        if(empty($t[1])){
            return'';
        }
        $t=strtolower($t[1]);
        if($t=='163.com'){
            return 'mail.163.com';
        }else if($t=='vip.163.com'){
            return 'vip.163.com';
        }else if($t=='126.com'){
            return 'mail.126.com';
        }else if($t=='qq.com'||$t=='vip.qq.com'||$t=='foxmail.com'){
            return 'mail.qq.com';
        }else if($t=='gmail.com'){
            return 'mail.google.com';
        }else if($t=='sohu.com'){
            return 'mail.sohu.com';
        }else if($t=='tom.com'){
            return 'mail.tom.com';
        }else if($t=='vip.sina.com'){
            return 'vip.sina.com';
        }else if($t=='sina.com.cn'||$t=='sina.com'){
            return 'mail.sina.com.cn';
        }else if($t=='tom.com'){
            return 'mail.tom.com';
        }else if($t=='yahoo.com.cn'||$t=='yahoo.cn'){
            return 'mail.cn.yahoo.com';
        }else if($t=='tom.com'){
            return 'mail.tom.com';
        }else if($t=='yeah.net'){
            return 'www.yeah.net';
        }else if($t=='21cn.com'){
            return 'mail.21cn.com';
        }else if($t=='hotmail.com'){
            return 'www.hotmail.com';
        }else if($t=='sogou.com'){
            return 'mail.sogou.com';
        }else if($t=='188.com'){
            return 'www.188.com';
        }else if($t=='139.com'){
            return 'mail.10086.cn';
        }else if($t=='189.cn'){
            return 'webmail15.189.cn/webmail';
        }else if($t=='wo.com.cn'){
            return 'mail.wo.com.cn/smsmail';
        }else if($t=='139.com'){
            return 'mail.10086.cn';
        }else {
            return '';
        }
    }
}